package com.xmad.ksadutils.AdSDK.AdMold.Pangolin;

import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;

import androidx.annotation.MainThread;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.TTSplashAd;
import com.xmad.ksadutils.AdSDK.AdBase.AdBaseLoad;
import com.xmad.ksadutils.AdSDK.AdBase.AdCode;
import com.xmad.ksadutils.AdSDK.Utlis.RxLogUtils;

public class PGLoadSplashScreenAd extends AdBaseLoad {
    private volatile static PGLoadSplashScreenAd mInstance;
    /**
     * 单例模式
     *
     * @return
     */
    public static PGLoadSplashScreenAd getInstance() {
        if (mInstance == null) {
            synchronized (PGLoadSplashScreenAd.class) {
                if (mInstance == null) {
                    mInstance = new PGLoadSplashScreenAd();
                }
            }
        }
        return mInstance;
    }

    @Override
    public void Show(final Activity activity, final FrameLayout frameLayout) {
        Log.e(TAG,"开屏开始加载");
        if (AdCode.getKsLoadSplashScreenId().equals("")&&PGAdId==null) {
            RxLogUtils.e("还未设置ID");
            return;
        }
        TTAdNative mTTAdNative = TTAdSdk.getAdManager().createAdNative(activity);
        if (ToastCode){
            Toast.makeText(activity,!PGAdId.equals("") ? PGAdId: AdCode.getLoadSplashScreenId(), Toast.LENGTH_SHORT).show();
        }
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(!PGAdId.equals("") ? PGAdId: AdCode.getLoadSplashScreenId())
                .setImageAcceptedSize(1080, 1920)
                .build();
        mTTAdNative.loadSplashAd(adSlot, new TTAdNative.SplashAdListener() {
            //请求广告失败
            @Override
            @MainThread
            public void onError(int code, String message) {
                //开发者处理跳转到APP主页面逻辑
                if (listener!=null){
                    //开发者处理跳转到APP主页面逻辑
                    listener.onJump(400);
                    Log.e("onError",code+message);
                }
            }

            //请求广告超时
            @Override
            @MainThread
            public void onTimeout() {
                //开发者处理跳转到APP主页面逻辑
                RxLogUtils.e("onTimeout");
                if (listener!=null){
                    //开发者处理跳转到APP主页面逻辑
                    listener.onJump(800);
                }

            }

            //请求广告成功
            @Override
            @MainThread
            public void onSplashAdLoad(TTSplashAd ad) {
                if (ad == null) {
                    return;
                }
                //获取SplashView
                View view = ad.getSplashView();
                ad.startClickEye();
                if (view != null && frameLayout != null && !activity.isFinishing()) {
                    frameLayout.removeAllViews();
                    //把SplashView 添加到ViewGroup中,注意开屏广告view：width =屏幕宽；height >=75%屏幕高
                    frameLayout.addView(view);
                    //设置不开启开屏广告倒计时功能以及不显示跳过按钮,如果这么设置，您需要自定义倒计时逻辑
                    //ad.setNotAllowSdkCountdown();
                }else {
                    //开发者处理跳转到APP主页面逻辑
                    if (listener!=null){
                        listener.onJump(201);
                    }
                }
                ad.setSplashInteractionListener(new TTSplashAd.AdInteractionListener() {
                    //点击回调
                    @Override
                    public void onAdClicked(View view, int type) {
                        RxLogUtils.e("onAdClicked");
                        if (listener!=null){
                            listener.onAdClicked();
                        }
                    }
                    //展示回调
                    @Override
                    public void onAdShow(View view, int type) {
                        Log.e(TAG,"开屏显示成功");
                    }
                    //跳过回调
                    @Override
                    public void onAdSkip() {
                        RxLogUtils.e("onAdSkip");
                        if (listener!=null){
                            //开发者处理跳转到APP主页面逻辑
                            listener.onJump(202);
                        }

                    }

                    //超时倒计时结束
                    @Override
                    public void onAdTimeOver() {
                        RxLogUtils.e("onAdTimeOver");
                        if (listener!=null){
                            //开发者处理跳转到APP主页面逻辑
                            listener.onJump(200);
                            Log.e(TAG,"开屏倒计时结束");
                        }
                    }
                });
            }
        }, 5000);
    }

}
